package com.jk.smartelderlycare.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.jk.smartelderlycare.dto.SysHomestay;
import com.jk.smartelderlycare.dto.SysSlider;
import com.jk.smartelderlycare.dto.sysVideo;
import com.jk.smartelderlycare.pojo.PagedResponse;
import com.jk.smartelderlycare.pojo.ResponseResult;
import com.jk.smartelderlycare.service.SysHomestayServiceFeign;
import com.jk.smartelderlycare.util.FileUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.Workbook;
import org.elasticsearch.common.recycler.Recycler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;

/**
 * @author hhk
 * @version 1.0
 * @ClassName SysHomestayController
 * @program: smart-elderly-care
 * @description: TODO
 * @date 2024/11/25 17:10
 */
@Api(tags = "民宿管理系统")
@Slf4j
@RestController
@RequestMapping(value = "/sysHomestay")
public class SysHomestayController {
    @Autowired
    private SysHomestayServiceFeign sysHomestayServiceFeign;
    /**
     * @description: 民宿信息列表后端分页查询
     * @param: [sysHomestay, pageNum, pageSize]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.pojo.PagedResponse<com.jk.smartelderlycare.dto.SysHomestay>>
     * @author hhk
     * @date: 2024/11/25 17:16
     */
    @ApiOperation(value = "民宿信息列表后端分页查询",httpMethod = "POST",notes = "民宿信息列表后端分页查询")
    @PostMapping(value = "findSysHomestayPaginationList")
    private ResponseResult<PagedResponse<SysHomestay>> findSysHomestayPaginationList(@RequestBody SysHomestay sysHomestay, @RequestParam(value = "pageNum") Integer pageNum,@RequestParam(value = "pageSize") Integer pageSize){
        return new ResponseResult<>(200,"查询成功",sysHomestayServiceFeign.findSysHomestayPaginationList(sysHomestay,pageNum,pageSize));
    }
    /**
     * @description: 民宿信息新增
     * @param: [sysHomestay]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/25 17:18
     */
    @ApiOperation(value = "民宿信息新增",httpMethod = "POST",notes = "民宿信息新增")
    @PostMapping(value = "addSysHomestay")
    private ResponseResult addSysHomestay(@RequestBody SysHomestay sysHomestay){
        try {
                sysHomestay.setCreateTime(new Date());
            sysHomestayServiceFeign.addSysHomestay(sysHomestay);
            return new ResponseResult(200,"添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"添加失败");
        }
    }
    /**
     * @description: 根据id删除民宿信息
     * @param: [homestayId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/25 17:20
     */
    @ApiOperation(value = "根据id删除民宿信息",httpMethod = "DELETE",notes = "根据id删除民宿信息")
    @DeleteMapping(value = "deleteSysHomestayById")
    private ResponseResult deleteSysHomestayById(@RequestParam(value = "homestayId") Long homestayId){
        try {
            sysHomestayServiceFeign.deleteSysHomestayById(homestayId);
            return new ResponseResult(200,"删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"删除失败");
        }
    }
    /**
     * @description: 根据id查询民宿信息
     * @param: [homestayId]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult<com.jk.smartelderlycare.dto.SysHomestay>
     * @author hhk
     * @date: 2024/11/25 17:21
     */
    @ApiOperation(value = "根据id查询民宿信息",httpMethod = "GET",notes = "根据id查询民宿信息")
    @GetMapping(value = "findSysHomestayById")
    private ResponseResult<SysHomestay> findSysHomestayById(@RequestParam(value = "Long") Long homestayId){
            return new ResponseResult<>(200,"查询成功",sysHomestayServiceFeign.findSysHomestayById(homestayId));
    }
    /**
     * @description: 修改民宿信息
     * @param: [sysHomestay]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/25 17:23
     */
    @ApiOperation(value = "修改民宿信息",httpMethod = "PUT",notes = "修改民宿信息")
    @PutMapping(value = "updateSysHomestay")
    private ResponseResult updateSysHomestay(@RequestBody SysHomestay sysHomestay){
        try {
            sysHomestay.setModifyTime(new Date());
            sysHomestayServiceFeign.updateSysHomestay(sysHomestay);
            return new ResponseResult(200,"修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"修改失败");
        }
    }
    /**
     * @description: 审核通过
     * @param: [sysHomestay]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/25 17:24
     */
    @ApiOperation(value = "审核通过",httpMethod = "PUT",notes = "审核通过")
    @PutMapping(value = "upPassStatusById")
    private ResponseResult upPassStatusById(@RequestBody SysHomestay sysHomestay){
        try {
            sysHomestayServiceFeign.upPassStatusById(sysHomestay);
            return new ResponseResult(200,"审核通过");
        } catch (Exception e) {
           e.printStackTrace();
           return new ResponseResult(500,"审核失败");
        }
    }
    /** 
     * @description: 审核未通过
     * @param: [sysHomestay]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/25 18:55
     */ 
    @ApiOperation(value = "审核未通过",httpMethod = "PUT",notes = "审核未通过")
    @PutMapping(value = "FailedStatusById")
    private ResponseResult FailedStatusById(@RequestBody SysHomestay sysHomestay){
        try {
            sysHomestayServiceFeign.FailedStatusById(sysHomestay);
            return new ResponseResult(200,"审核失败");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(500,"审核失败");
        }
    }

    /**
     * @description: 上传图片
     * @param: [file, request]
     * @return: java.lang.String
     * @author hhk
     * @date: 2024/11/7 10:54
     */
    @ApiOperation(value = "上传图片",httpMethod = "POST",notes = "上传图片")
    @PostMapping("uploadImg")
    public String uploadImg(MultipartFile file, HttpServletRequest request) {
        return FileUtil.uploadFile(file,request);
    }

    /**
     * @description: 信息导出
     * @param: [video, response]
     * @return: void
     * @author hhk
     * @date: 2024/11/15 11:38
     */
    @ApiOperation(value = "信息导出", httpMethod = "POST", notes = "信息导出")
    @RequestMapping("/exportExcel")
    public void exportExcel(@RequestBody SysHomestay sysHomestay, HttpServletResponse response){
        List<SysHomestay> HomestayList = sysHomestayServiceFeign.fimdSysHomestayList();

        //.xlsx格式
//        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        //.xls格式
//        response.setContentType("application/vnd.ms-excel;charset=utf-8");

        response.setContentType("application/octet-stream; charset=UTF-8");


        response.setCharacterEncoding("utf-8");
        //前端存在跨域不成功，设置可访问
        response.setHeader("Access-Control-Allow-Origin","*");
        //设置不要缓存
        response.setHeader("Pragma", "No-cache");
        try {
            // 这里URLEncoder.encode可以防止中文乱码
            String fileName = URLEncoder.encode("postsInfo", String.valueOf(StandardCharsets.UTF_8));
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
            //设置sheet名
            ExportParams params = new ExportParams();
            params.setSheetName("民宿信息表");
            //设置标题
            params.setTitle("民宿信息");
            // 这里需要设置不关闭流
            Workbook workbook = ExcelExportUtil.exportExcel(params, SysHomestay.class, HomestayList);
            //输出流
            OutputStream outStream = response.getOutputStream();
            //浏览器下载
            workbook.write(outStream);
            //关闭流
            outStream.flush();
            outStream.close();
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
    /**
     * @description: 信息导入
     * @param: [file]
     * @return: com.jk.smartelderlycare.pojo.ResponseResult
     * @author hhk
     * @date: 2024/11/15 11:38
     */
    @ApiOperation(value = "信息导入", httpMethod = "POST", notes = "信息导入")
    @PostMapping(value = "importExcel")
    public ResponseResult importExcel(@RequestPart("file") MultipartFile file) {
        try {
            ImportParams params = new ImportParams();
            params.setHeadRows(2);
            List<SysHomestay> list = ExcelImportUtil.importExcel(file.getInputStream(), SysHomestay.class, params);
            list.forEach(sysHomestay -> {

                sysHomestayServiceFeign.addHomestay(sysHomestay);
            });
            return new ResponseResult(200, "导入用户信息成功！");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseResult(444, "导入用户信息失败！");
        }
    }










}
